--[[
    桌子上的牌
--]]

local List = require "games.texas.rule.list"

local math_random = math.random
local table_insert = table.insert

local M = class("ccard")

--  2 - A
--  2     3     4     5     6     7     8     9     10    11    12    13    14
M.s_pais = {
    0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,   -- spades  黑桃
    0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,   -- hearts 红心
    0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,   -- clubs 梅花
    0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E,   -- diamonds 方块
}
M.s_mask_color = 0xF0
M.s_mask_value = 0x0F

function M:ctor()


end

--取值
function M:getVal(a)
    return math.tointeger(a & self.s_mask_value)
end

--取花色
function M:getColor(a)
    return math.tointeger((a & self.s_mask_color)/16 + 1)
end

-- 返回1-指定区间的一个随机数
function M:random(min, max)
    if min < max then return min end
    for i = 1, 2 do
        math_random(max) -- 增加随机几率
    end
    return math_random(min, max)
end

--洗牌
function M:shuffleForRand(ignore_cards)
    local tempCards = {}
    if not ignore_cards then ignore_cards = {} end
    for _, v in ipairs(self.s_pais) do
        if table.has(ignore_cards, v) then
        else
            table_insert(tempCards, v)
        end
    end
    tempCards = self:randExchangeCards(tempCards)
    self.cards = List.new()
    for _, v in ipairs(tempCards) do
        List.pushBack(self.cards, v)
    end
end

--随机交换
function M:randExchangeCards(cards)
    if not next(cards) then return cards end
    --local time_start = g.time()
    local idx = 0
    local newCard = 0
    local len = #cards
    --打乱牌有多种方式，随机一种
    local func1 = function()
        for i = 1, len do
            idx = math_random(i, len)
            if i ~= idx then
                newCard = cards[idx]
                cards[idx] = cards[i]
                cards[i] = newCard
            end
        end
    end
    local func2 = function()
        for i = len, 1, -1 do
            idx = math_random(1, i)
            if i ~= idx then
                newCard = cards[idx]
                cards[idx] = cards[i]
                cards[i] = newCard
            end
        end
    end
    local func3 = function()
        --这个方法不会把牌全打乱
        for i = len, 1, -1 do
            idx = math_random(i, len)
            if i ~= idx then
                newCard = cards[idx]
                cards[idx] = cards[i]
                cards[i] = newCard
            end
        end
    end
    local index = math_random(1,6)
    local funcs = {func1, func2, func1, func2, func1, func2}
    funcs[index]()
    --g.log:debug("randExchangeCards:", g.tid, index, g.time() - time_start)
    return cards
end

--把多取的牌放回列表
function M:reSaveCards(cards)
    cards = self:randExchangeCards(cards)
    for _, v in ipairs(cards) do
        List.pushBack(self.cards, v)
    end
end

--取列表的牌
function M:getNextCards(count)
    local cards = {}
    count = count or 0
    if count <= 0 then return cards end
    for i = 1, count do
        table_insert(cards, List.popFront(self.cards))
    end
    return cards
end

--取公牌
function M:getCommonCards()
    return self:getNextCards(5)
end

function M:removeCard(card)
    return List.popValue(self.cards, card)
end

return M